﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CustomersSuppliersDataLayer;
using CustomersSuppliersBusinessLayer;

namespace CustomersSuppliersWebApplication
{
    public class CashVariables
    {
        public static BLDealersTypeCollection DealersTypeCollection
        {
            get
            {
                BLDealersTypeCollection DealersTypeCollection = (BLDealersTypeCollection)System.Web.HttpContext.Current.Cache["DealersTypeCollection"];
                if (DealersTypeCollection == null)
                {
                    DealersTypeCollection = new BLDealersTypeCollection();
                    DealersTypeCollection.Template.AddSort(BLDealersType.PropertiesNames.DealerType_NameAr,false);
                    DealersTypeCollection.Load();
                    System.Web.HttpContext.Current.Cache["DealersTypeCollection"] = DealersTypeCollection;
                }
                return DealersTypeCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["DealersTypeCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("DealersTypeCollection");
                }
            }
        }

        public static BLDealersClassificationCollection DealersClassificationCollection
        {
            get
            {
                BLDealersClassificationCollection DealersClassificationCollection = (BLDealersClassificationCollection)System.Web.HttpContext.Current.Cache["DealersClassificationCollection"];
                if (DealersClassificationCollection == null)
                {
                    DealersClassificationCollection = new BLDealersClassificationCollection();
                    DealersClassificationCollection.Template.AddSort(BLDealersClassification.PropertiesNames.DealerClassification_NameAr, false);
                    DealersClassificationCollection.Load();
                    System.Web.HttpContext.Current.Cache["DealersClassificationCollection"] = DealersClassificationCollection;
                }
                return DealersClassificationCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["DealersClassificationCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("DealersClassificationCollection");
                }
            }
        }
        
        public static BLImportanceCollection ImportanceCollection
        {
            get
            {
                BLImportanceCollection ImportanceCollection = (BLImportanceCollection)System.Web.HttpContext.Current.Cache["ImportanceCollection"];
                if (ImportanceCollection == null)
                {
                    ImportanceCollection = new BLImportanceCollection();
                    ImportanceCollection.Template.AddSort(BLImportance.PropertiesNames.Importance_NameAr, false);
                    ImportanceCollection.Load();
                    System.Web.HttpContext.Current.Cache["ImportanceCollection"] = ImportanceCollection;
                }
                return ImportanceCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["ImportanceCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("ImportanceCollection");
                }
            }
        }

        public static BLTypeOfTradeCollection TypeOfTradeCollection
        {
            get
            {
                BLTypeOfTradeCollection TypeOfTradeCollection = (BLTypeOfTradeCollection)System.Web.HttpContext.Current.Cache["TypeOfTradeCollection"];
                if (TypeOfTradeCollection == null)
                {
                    TypeOfTradeCollection = new BLTypeOfTradeCollection();
                    TypeOfTradeCollection.Template.AddSort(BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameAr, false);
                    TypeOfTradeCollection.Load();
                    System.Web.HttpContext.Current.Cache.Remove("TypeOfTradeCollection");
                }
                return TypeOfTradeCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["TypeOfTradeCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("TypeOfTradeCollection");
                }
            }
        }

        public static BLProductCollection ProductCollection
        {
            get
            {
                BLProductCollection ProductCollection = (BLProductCollection)System.Web.HttpContext.Current.Cache["ProductCollection"];
                if (ProductCollection == null)
                {
                    ProductCollection = new BLProductCollection();
                    ProductCollection.Template.AddSort(BLProduct.PropertiesNames.Product_NameAr, false);
                    ProductCollection.Load();
                    System.Web.HttpContext.Current.Cache["ProductCollection"] = ProductCollection;
                }
                return ProductCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["ProductCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("ProductCollection");
                }
            }
        }

        public static BLProductsClassificationCollection ProductsClassificationCollection
        {
            get
            {
                BLProductsClassificationCollection ProductsClassificationCollection = (BLProductsClassificationCollection)System.Web.HttpContext.Current.Cache["ProductsClassificationCollection"];
                if (ProductsClassificationCollection == null)
                {
                    ProductsClassificationCollection = new BLProductsClassificationCollection();
                    ProductsClassificationCollection.Template.AddSort(BLProductsClassification.PropertiesNames.ProductClassification_NameAr, false);
                    ProductsClassificationCollection.Load();
                    System.Web.HttpContext.Current.Cache["ProductsClassificationCollection"] = ProductsClassificationCollection;
                }
                return ProductsClassificationCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["ProductsClassificationCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("ProductsClassificationCollection");
                }
            }
        }

        public static BLContactTypeCollection ContactTypeCollection
        {
            get
            {
                BLContactTypeCollection ContactTypeCollection = (BLContactTypeCollection)System.Web.HttpContext.Current.Cache["ContactTypeCollection"];
                if (ContactTypeCollection == null)
                {
                    ContactTypeCollection = new BLContactTypeCollection();
                    ContactTypeCollection.Template.AddSort(BLContactType.PropertiesNames.ContactType_NameAr, false);
                    ContactTypeCollection.Load();
                    System.Web.HttpContext.Current.Cache["ContactTypeCollection"] = ContactTypeCollection;
                }
                return ContactTypeCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["ContactTypeCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("ContactTypeCollection");
                }
            }
        }

        public static BLCountryCollection CountryCollection
        {
            get
            {
                BLCountryCollection CountryCollection = (BLCountryCollection)System.Web.HttpContext.Current.Cache["CountryCollection"];
                if (CountryCollection == null)
                {
                    CountryCollection = new BLCountryCollection();
                    CountryCollection.Template.AddSort(BLCountry.PropertiesNames.Country_NameAr, false);
                    CountryCollection.Load();
                    System.Web.HttpContext.Current.Cache["CountryCollection"] = CountryCollection;
                }
                return CountryCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["CountryCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("CountryCollection");
                }
            }
        }

        public static BLJobCollection JobCollection
        {
            get
            {
                BLJobCollection JobCollection = (BLJobCollection)System.Web.HttpContext.Current.Cache["JobCollection"];
                if (JobCollection == null)
                {
                    JobCollection = new BLJobCollection();
                    JobCollection.Template.AddSort(BLJob.PropertiesNames.Job_NameAr, false);
                    JobCollection.Load();
                    System.Web.HttpContext.Current.Cache["JobCollection"] = JobCollection;
                }
                return JobCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["JobCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("JobCollection");
                }
            }
        }

        public static BLBankCollection BankCollection
        {
            get
            {
                BLBankCollection BankCollection = (BLBankCollection)System.Web.HttpContext.Current.Cache["BankCollection"];
                if (BankCollection == null)
                {
                    BankCollection = new BLBankCollection();
                    BankCollection.Template.AddSort(BLBank.PropertiesNames.Bank_NameAr, false);
                    BankCollection.Load();
                    System.Web.HttpContext.Current.Cache["BankCollection"] = BankCollection;
                }
                return BankCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["BankCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("BankCollection");
                }
            }
        }
        
        public static BLMaritalStatuCollection MaritalStatuCollection
        {
            get
            {
                BLMaritalStatuCollection MaritalStatusCollection = (BLMaritalStatuCollection)System.Web.HttpContext.Current.Cache["BLMaritalStatuCollection"];
                if (MaritalStatusCollection == null)
                {
                    MaritalStatusCollection = new BLMaritalStatuCollection();
                    MaritalStatusCollection.Template.AddSort(BLMaritalStatu.PropertiesNames.MaritalStatus_NameAr, false);
                    MaritalStatusCollection.Load();
                    System.Web.HttpContext.Current.Cache["MaritalStatuCollection"] = MaritalStatusCollection;
                }
                return MaritalStatusCollection;
            }
            set
            {
                if (value != null)
                {
                    System.Web.HttpContext.Current.Cache["MaritalStatuCollection"] = value;
                }
                else
                {
                    System.Web.HttpContext.Current.Cache.Remove("MaritalStatuCollection");
                }
            }
        }

        public static void InitializeCash()
        {
            BLDealersType.AfterInsertGlobal += afterModifications;
            BLDealersType.AfterUpdateGlobal += afterModifications;
            BLDealersType.AfterDeleteGlobal += afterModifications;

            BLDealersClassification.AfterInsertGlobal += afterModifications;
            BLDealersClassification.AfterUpdateGlobal += afterModifications;
            BLDealersClassification.AfterDeleteGlobal += afterModifications;

            BLImportance.AfterInsertGlobal += afterModifications;
            BLImportance.AfterUpdateGlobal += afterModifications;
            BLImportance.AfterDeleteGlobal += afterModifications;

            BLTypeOfTrade.AfterInsertGlobal += afterModifications;
            BLTypeOfTrade.AfterUpdateGlobal += afterModifications;
            BLTypeOfTrade.AfterDeleteGlobal += afterModifications;

            BLProductsClassification.AfterInsertGlobal += afterModifications;
            BLProductsClassification.AfterUpdateGlobal += afterModifications;
            BLProductsClassification.AfterDeleteGlobal += afterModifications;

            BLProduct.AfterInsertGlobal += afterModifications;
            BLProduct.AfterUpdateGlobal += afterModifications;
            BLProduct.AfterDeleteGlobal += afterModifications;

            BLContactType.AfterInsertGlobal += afterModifications;
            BLContactType.AfterUpdateGlobal += afterModifications;
            BLContactType.AfterDeleteGlobal += afterModifications;

            BLCountry.AfterInsertGlobal += afterModifications;
            BLCountry.AfterUpdateGlobal += afterModifications;
            BLCountry.AfterDeleteGlobal += afterModifications;

            BLJob.AfterInsertGlobal += afterModifications;
            BLJob.AfterUpdateGlobal += afterModifications;
            BLJob.AfterDeleteGlobal += afterModifications;

            BLBank.AfterInsertGlobal += afterModifications;
            BLBank.AfterUpdateGlobal += afterModifications;
            BLBank.AfterDeleteGlobal += afterModifications;

            BLMaritalStatu.AfterInsertGlobal += afterModifications;
            BLMaritalStatu.AfterUpdateGlobal += afterModifications;
            BLMaritalStatu.AfterDeleteGlobal += afterModifications;


        }

        private static void afterModifications(object sender, EventArgs e)
        {
            if (sender is BLDealersType)
            {
                CashVariables.DealersTypeCollection = null;
            }
            else if (sender is BLDealersClassification)
            {
                CashVariables.DealersClassificationCollection = null;
            }
            else if(sender is BLImportance)
            {
                CashVariables.ImportanceCollection = null;
            }
            else if (sender is BLTypeOfTrade)
            {
                CashVariables.TypeOfTradeCollection = null;
            }
            else if (sender is BLProductsClassification)
            {
                CashVariables.ProductsClassificationCollection = null;
            }
            else if (sender is BLProduct)
            {
                CashVariables.ProductCollection = null;
            }
            else if (sender is BLContactType)
            {
                CashVariables.ContactTypeCollection = null;
            }
            else if (sender is BLCountry)
            {
                CashVariables.CountryCollection = null;
            }
            else if (sender is BLJob)
            {
                CashVariables.JobCollection = null;
            }
            else if (sender is BLBank)
            {
                CashVariables.BankCollection = null;
            }
            else if (sender is BLMaritalStatu)
            {
                CashVariables.MaritalStatuCollection = null;
            }

        }
    }
}